<?php
namespace admin\model;

use admin\model\Model;

use admin\model\PasswordHash;

class LoginModel extends Model{
    
    /**
     * 判断用户名和密码是否正确
     */
    public function checkInfo($params = null){
        $sql = 'SELECT id,password,portrait,email,create_time,state  FROM user 
        WHERE is_manager = 1 AND name = :name AND email = :email';
        $user = $this->p_query($sql,array('name'=>$params['name'],'email'=>$params['email']));
        $data = [];
        $data['code'] = 0;
        if($user){
            if($user['state'] == 1){
                $this->chageState(array('id'=>$user['id']));        //强制下线
            }else{
                $hasher = new PasswordHash(8, false); // 初始化散列器为不可移植(这样更安全)
                //第一个参数加密前的（明文）密码，第二个参数是加密过的密码
                $pass = $params['password'].$user['create_time'];
                if($hasher->CheckPassword($pass, $user['password'])){
                    $data['code'] = 1;
                    $data['info'] = $user;
                    $this->chageState(array('state'=>1,'id'=>$user['id']));     //更新登陆状态
                }
            }
        }
        return $data;
    }
    /**
     * 更新管理员状态
     */
    public function chageState($params){
        if($params['state']==1){
            $curr = time();
            $sql = "UPDATE user SET state = 1,last_time = {$curr} WHERE id = {$params['id']} ";
        }else{
            $sql = "UPDATE user SET state = 0 WHERE id = {$params['id']} ";
        }
        $this->execute($sql);
        return $this->db->num;
    }
}